package com.ld.hpfs.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.hpfs.pojo.DbSourceOutting;
import com.ld.hpfs.service.DbSourceOuttingService;
import com.ld.hpfs.dao.DbSourceOuttingMapper;

@Service
public class DbSourceOuttingServiceImpl implements DbSourceOuttingService {

	private static final Logger logger = LoggerFactory.getLogger(PlanServiceImpl.class);
	@Autowired
	private DbSourceOuttingMapper dbsourceOuttingMapper;

	public int addDbSourceOutting(DbSourceOutting outting) {
		
		java.util.HashMap<String, Object> params = new java.util.HashMap<String, Object>();
//		params.put("DS_TYPE", setting.getDbsourceType());
//		params.put("DS_DB_SID", setting.getInstance());
//		params.put("DS_USERNAME", setting.getDbusername());
//		params.put("DS_PASSWORD", setting.getDbpassword());
//		params.put("DS_IP", setting.getDbhostname());
//		params.put("DS_PORT", setting.getDbport());
//		params.put("DS_CONNECT_STR", setting.getDbconnectstring());
//		params.put("MAPPING_STR", setting.getDbcolumnMapping());
//		params.put("COMPANY_ID", String.valueOf(setting.getCompanyId()));
//		params.put("DS_NAME", setting.getDbdesc());
//		params.put("SQL_STR", setting.getDbsql());
//		params.put("CREATOR_ID", setting.getCreatorId()); // TODO: GET// CREATOR_ID

		params.put("DS_HID", outting.getDs_hid());
		params.put("HVERSION", outting.getHversion());
		params.put("DS_NAME", outting.getDs_name());
		params.put("DS_TYPE", outting.getDs_type());
		params.put("DS_DB_NAME", outting.getDs_db_name());
		params.put("DS_DB_SID", outting.getDs_db_sid());
		params.put("DS_USERNAME", outting.getDs_username());
		params.put("DS_PASSWORD", outting.getDs_password());
		params.put("DS_IP", outting.getDs_ip());
		params.put("DS_PORT", outting.getDs_port());
		params.put("SQL_STR", outting.getSql_str());
		params.put("DS_CONNECT_STR", outting.getDs_connect_str());
		params.put("CREATOR_ID", outting.getCreator_id());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// format.setTimeZone(TimeZone.getTimeZone( "GMT-0"));
		String date = format.format(new Date());
		params.put("CREATE_TIME", date);

		return dbsourceOuttingMapper.addDbSourceOutting(params);

	}

	public  ArrayList<HashMap<String,Object>> getDbSourceOuttings(HashMap<String,Object> param){
		int count = dbsourceOuttingMapper.getListCount(param);
		if(count == 0){
			return null;
		}else{
			param.put("pageCount", count);
			return dbsourceOuttingMapper.getDbSourceOuttings(param);
		}
	}

	public DbSourceOutting getDbSourceOutting(int id) {
		HashMap<String, Object> beanMap = dbsourceOuttingMapper.getDbSourceOutting(id);

		if (beanMap != null) {
			DbSourceOutting dboutting = new DbSourceOutting();
			dboutting.setDs_hid(Integer.parseInt(beanMap.get("DS_HID").toString()));
			dboutting.setHversion(Integer.parseInt(beanMap.get("HVERSION").toString()));
			dboutting.setDs_name(beanMap.get("DS_NAME").toString());
			dboutting.setDs_type(beanMap.get("DS_TYPE").toString());
			dboutting.setDs_db_name(beanMap.get("DS_DB_NAME").toString());
			dboutting.setDs_db_sid(beanMap.get("DS_DB_SID").toString());
			dboutting.setDs_username(beanMap.get("DS_USERNAME").toString());
			dboutting.setDs_password(beanMap.get("DS_PASSWORD").toString());
			dboutting.setDs_ip(beanMap.get("DS_IP").toString());
			dboutting.setDs_port(beanMap.get("DS_PORT").toString());
			dboutting.setDs_connect_str(beanMap.get("DS_CONNECT_STR").toString());
			dboutting.setSql_str(beanMap.get("SQL_STR").toString());
			dboutting.setCreator_id(beanMap.get("CREATOR_ID").toString());
			dboutting.setCreate_time(beanMap.get("CREATE_TIME").toString());
			return dboutting;
		}

		return null;

	}

	public int deleteDbSourceOutting(int id) {
		return dbsourceOuttingMapper.deleteDbSourceOutting(id);

	}

	public int updateDbSourceOutting(DbSourceOutting outting) {
		java.util.HashMap<String, Object> params = new java.util.HashMap<String, Object>();
//		params.put("DS_TYPE", setting.getDbsourceType());
//		params.put("DS_DB_SID", setting.getInstance());
//		params.put("DS_USERNAME", setting.getDbusername());
//		params.put("DS_PASSWORD", setting.getDbpassword());
//		params.put("DS_IP", setting.getDbhostname());
//		params.put("DS_PORT", setting.getDbport());
//		params.put("DS_CONNECT_STR", setting.getDbconnectstring());
//		params.put("MAPPING_STR", setting.getDbcolumnMapping());
//		params.put("COMPANY_ID", String.valueOf(setting.getCompanyId()));
//		params.put("DS_NAME", setting.getDbdesc());
//		params.put("DS_HID", setting.getId().toString());
		

		params.put("DS_HID", outting.getDs_hid().toString());
		params.put("HVERSION", outting.getHversion());
		params.put("DS_NAME", outting.getDs_name());
		params.put("DS_TYPE", outting.getDs_type());
		params.put("DS_DB_NAME", outting.getDs_db_name());
		params.put("DS_DB_SID", outting.getDs_db_sid());
		params.put("DS_USERNAME", outting.getDs_username());
		params.put("DS_PASSWORD", outting.getDs_password());
		params.put("DS_IP", outting.getDs_ip());
		params.put("DS_PORT", outting.getDs_port());
		params.put("DS_CONNECT_STR", outting.getDs_connect_str());
		params.put("SQL_STR", outting.getSql_str());
		
		return dbsourceOuttingMapper.updateDbSourceOutting(params);

	}

	public int setQuoteExportSource(HashMap<String,Object> param){
		try{
			dbsourceOuttingMapper.setQuoteExportSource(param);
			return 0;
		}catch(Exception ex){
			logger.error(ex.getMessage());
			return -1;
		}
	}
	
	public int getDbOuttingERPID(int id){
		try{
			return dbsourceOuttingMapper.getDbOuttingERPID(id);
		}catch(Exception ex){
			logger.error(ex.getMessage());
			return -1;
		}
	}

}
